import ObsClient from 'esdk-obs-browserjs';
// 上传华为云数据
function uploadOBS(fileDir, SourceFile) {
  let obsClient = new ObsClient({
    access_key_id: 'LIPKI8IPDE9GZDXAUJLI',
    secret_access_key: 'qEBUIdd1smDmOPPDpEvAo3u64XLXwBDTgaqGkIvE',
    server: 'https://obs.cn-east-3.myhuaweicloud.com'
  });

  obsClient.putObject(
    {
      Bucket: 'saas-information',
      Key: fileDir,
      SourceFile: SourceFile // 文件
    },
    function (err, result) {
      if (err) {
        console.log('上传失败');
      } else {
        console.log('上传成功');
      }
    }
  );
}
// 视频上传华为云数据
function videoUploadOBS(data) {}
// 删除华为云数据
function deleteOBS(data) {
  let obsClient = new ObsClient({
    access_key_id: 'LIPKI8IPDE9GZDXAUJLI',
    secret_access_key: 'qEBUIdd1smDmOPPDpEvAo3u64XLXwBDTgaqGkIvE',
    server: 'https://obs.cn-east-3.myhuaweicloud.com'
  });

  obsClient.deleteObject(
    {
      Bucket: 'saas-information',
      Key: data
    },
    function (err, result) {
      if (err) {
        console.log('Error-->' + err);
      } else {
        console.log('Status-->' + result.CommonMsg.Status);
      }
    }
  );
}
// 批量删除
function deleteAllOBS(Objects) {
  let obsClient = new ObsClient({
    access_key_id: 'LIPKI8IPDE9GZDXAUJLI',
    secret_access_key: 'qEBUIdd1smDmOPPDpEvAo3u64XLXwBDTgaqGkIvE',
    server: 'https://obs.cn-east-3.myhuaweicloud.com'
  });
  obsClient.deleteObjects(
    {
      Bucket: 'saas-information',
      // 设置为verbose模式
      Quiet: false,
      // Objects: [{ Key: 'objectname1' }, { Key: 'objectname2' }, { Key: 'objectname3' }]
      Objects: Objects
    },
    function (err, result) {
      if (err) {
        console.log('Error-->' + err);
      } else {
        console.log('Status-->' + result.CommonMsg.Status);
        // if (result.CommonMsg.Status < 300 && result.InterfaceResult) {
        // 获取删除成功的对象
        // console.log('Deleteds:');
        // for (let i in result.InterfaceResult.Deleteds) {
        //   console.log('Deleted[' + i + ']:');
        //   console.log('Key-->' + result.InterfaceResult.Deleteds[i]['Key']);
        //   console.log('VersionId-->' + result.InterfaceResult.Deleteds[i]['VersionId']);
        // }
        // 获取删除失败的对象
        // console.log('Errors:');
        // for (let i in result.InterfaceResult.Errors) {
        //   console.log('Error[' + i + ']:');
        //   console.log('Key-->' + result.InterfaceResult.Errors[i]['Key']);
        //   console.log('VersionId-->' + result.InterfaceResult.Errors[i]['VersionId']);
        // }
        // }
      }
    }
  );
}
// 计算时间差
function timeFns(dateBegin, dateEnd, state) {
  // 如果时间格式是正确的，那下面这一步转化时间格式就可以不用了
  let dateDiff = dateEnd - dateBegin; // 时间差的毫秒数
  let dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); // 计算出相差天数
  let leave1 = dateDiff % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
  let hours = Math.floor(leave1 / (3600 * 1000)); // 计算出小时数 // 计算相差分钟数
  let leave2 = leave1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
  let minutes = Math.floor(leave2 / (60 * 1000)); // 计算相差分钟数 // 计算相差秒数
  let timeFn = dayDiff + '天 ' + hours + '小时 ' + minutes + ' 分钟';
  if (state === 'identical') {
    return dayDiff + 1 + '天 ' + hours + '小时 ' + minutes + ' 分钟';
  } else {
    return timeFn;
  }
}
function handleCover(file) {
  // pdf
  if (file.file.type === 'application/pdf') {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/appendix_icon_pdf.png';
  } else if (
    // docx
    file.file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ||
    file.file.type === 'application/msword'
  ) {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/appendix_icon_word.png';
  } else if (
    // mp4
    file.file.type === 'video/mp4' ||
    file.file.type === 'video/avi'
  ) {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/appendix_icon_mp4.png';
  } else if (
    // xlsx
    file.file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ||
    file.file.type === 'application/vnd.ms-excel'
  ) {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/popup_icon_xlsx.png';
  } else if (
    // ppt
    file.file.type === 'application/vnd.ms-powerpoint' ||
    file.file.type === 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
  ) {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/appendix_icon_ppt.png';
  } else if (
    // txt
    file.file.type === 'text/plain'
  ) {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/popup_icon_txt.png';
  } else if (
    file.file.type === 'image/png' ||
    file.file.type === 'image/jpeg' ||
    file.file.type === 'png' ||
    file.file.type === 'jpeg' ||
    file.file.type === 'image/bmp'
  ) {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/appendix_icon_png.png';
  } else {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/popup_icon_tongyong.png';
  }
}
function getThumbUrl(item) {
  if (item.documentFormat === 'pdf') {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/appendix_icon_pdf.png';
  } else if (item.documentFormat === 'mp4' || item.documentFormat === 'avi') {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/appendix_icon_mp4.png';
  } else if (item.documentFormat === 'docx' || item.documentFormat === 'doc') {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/appendix_icon_word.png';
  } else if (item.documentFormat === 'txt') {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/popup_icon_txt.png';
  } else if (item.documentFormat === 'xlsx' || item.documentFormat === 'xls') {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/popup_icon_xlsx.png';
  } else if (item.documentFormat === 'ppt' || item.documentFormat === 'pptx') {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/appendix_icon_ppt.png';
  } else if (
    item.documentFormat === 'png' ||
    item.documentFormat === 'jpg' ||
    item.documentFormat === 'jpeg' ||
    item.documentFormat === 'bmp'
  ) {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/appendix_icon_png.png';
  } else {
    return 'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/popup_icon_tongyong.png';
  }
}
let defaultFengmian = [
  'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/gonggao1.png',
  'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/gonggao4.png',
  'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/gonggao2.png',
  'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/gonggao8.png',
  'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/gonggao7.png',
  'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/gonggao33.png',
  'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/gonggao66.png',
  'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/gonggao9.png',
  'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/gonggao6.png',
  'https://saas-information.obs.cn-east-3.myhuaweicloud.com/assets/img/gonggao5.png'
]; // 默认封面图
// 处理转义url 截取
function getEscapeUrl(url, state) {
  // state(type=png;name=哈哈；url:完整地址; assetsUrl:timeurl)
  let urltime = url.substring(url.lastIndexOf('/') + 1); // 截取最后一个/后的所有字符（时间戳_名字.png）
  let first = url.substring(0, url.lastIndexOf('/') + 1); // 截取最后一个/前的所有字符（http://....2023-01-10/）
  let assetsUrl = first.slice(57); // assets/2022-01-20/16922....png

  let timeStamp = urltime.substring(0, 14); // 时间戳
  let nameType = urltime.substring(14); // 名字+类型（haha.png）
  let name = nameType.substring(0, nameType.lastIndexOf('.')); // 截取（haha.png）最后一个.前的所有字符（haha）
  let three = nameType.substring(nameType.lastIndexOf('.')); // 截取（haha.png）最后一个.后的所有字符（.png）
  // // console.log(1, urltime);
  // console.log(2, first);
  // console.log(3, assetsUrl);
  // console.log(4, timeStamp);
  // // console.log(nameType);
  // console.log(name);
  // console.log(three);

  if (state === 'type') {
    return three;
  } else if (state === 'assetsUrl') {
    return assetsUrl + urltime;
  } else if (state === 'name') {
    return name;
  } else {
    if (name.indexOf('%') !== -1) {
      return first + timeStamp + name + three;
    } else {
      return first + timeStamp + encodeURIComponent(name) + three;
    }
  }
}
// 求和
function getSum(arr) {
  let sum = 0;
  for (let i = arr.length - 1; i >= 0; i--) {
    sum += arr[i];
  }
  return sum;
}
// 数组去重保留最后一个
function arrayUnique(arr, name) {
  let hash = {};
  return arr.reduce((acc, cru, index) => {
    if (!hash[cru[name]]) {
      hash[cru[name]] = { index: acc.length };
      acc.push(cru);
    } else {
      acc.splice(hash[cru[name]]['index'], 1, cru);
    }
    return acc;
  }, []);
}
// 下载附件
function downloadURL(urls, name) {
  const download = new XMLHttpRequest();
  download.open('GET', urls, true);
  download.responseType = 'blob';
  download.onload = function () {
    const url = window.URL.createObjectURL(download.response);
    const a = document.createElement('a');
    a.href = url;
    a.download = name;
    a.click();
  };
  download.send();
}
// table 表头数据
let columns = [
  {
    title: '设备名称',
    dataIndex: 'equipmentName'
  },
  {
    title: '冲突模块',
    dataIndex: 'model',
    ellipsis: true,
    width: 104
  },
  {
    title: '发布人姓名',
    dataIndex: 'releaseUserName',
    ellipsis: true,
    width: 104
  },
  {
    title: '发布信息',
    dataIndex: 'releaseName',
    ellipsis: true
  },
  {
    title: '操作',
    dataIndex: 'operation',
    width: 104
  }
];

export {
  deleteOBS,
  uploadOBS,
  videoUploadOBS,
  deleteAllOBS,
  timeFns,
  handleCover,
  getEscapeUrl,
  getThumbUrl,
  getSum,
  arrayUnique,
  defaultFengmian,
  downloadURL,
  columns
};
